import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  UseInterceptors,
  UploadedFile,
  UploadedFiles,
  Session,
  Req,
} from '@nestjs/common';
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiConsumes,
} from '@nestjs/swagger';
import { Request } from 'express';
import { JwtAuthGuard } from '@wwjCommon/security/guards/jwt-auth.guard';
import { RolesGuard } from '@wwjCommon/security/guards/roles.guard';
import { Roles } from '@wwjCommon/security/decorators/roles.decorator';
import { Public } from '@wwjCommon/security/decorators/public.decorator';
import { BusinessException } from '@wwjCommon/exception/business.exception';
// @UploadedFile() - 单文件上传，配合 @UseInterceptors(FileInterceptor('file'))
// @UploadedFiles() - 多文件上传，配合 @UseInterceptors(FilesInterceptor('files'))
// @Session() - 获取会话对象，对应PHP Session::get()
// @Req() - 获取Request对象，对应PHP Request

/**
 * ScheduleController
 * 对应 PHP: Schedule Controller
 * 对应 Java: @RestController
 *
 * 支持装饰器：
 * - @UploadedFile() - 单文件上传 (对应PHP Request::file())
 * - @UploadedFiles() - 多文件上传
 * - @Session() - 会话管理 (对应PHP Session::get())
 * - @Req() - 请求对象 (对应PHP Request)
 */
@ApiTags('sys')
@Controller('adminapi/sys')
export class ScheduleController {
  constructor() {}

  /**
   * 自动任务
   * 路由: GET schedule/list
   * PHP路由: Route::get('schedule/list', 'sys.Schedule/lists')
   */
  @Get('schedule/list')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async lists(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: lists

      return await this.scheduleService.getPage(data);
    } catch (error) {
      throw new BusinessException('lists操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: GET schedule/template
   * PHP路由: Route::get('schedule/template', 'sys.Schedule/template')
   */
  @Get('schedule/template')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async template(): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: template

      return await this.scheduleService.getTemplateList();
    } catch (error) {
      throw new BusinessException('template操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: GET schedule/type
   * PHP路由: Route::get('schedule/type', 'sys.Schedule/getType')
   */
  @Get('schedule/type')
  @ApiOperation({ summary: '自动任务' })
  async getType() {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getType
      // 直接返回字典数据
      return ScheduleDict.getType();
    } catch (error) {
      throw new BusinessException('getType操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: GET schedule/:id
   * PHP路由: Route::get('schedule/:id', 'sys.Schedule/info')
   */
  @Get('schedule/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async info(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: info

      return await this.scheduleService.getInfo(id);
    } catch (error) {
      throw new BusinessException('info操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: POST schedule
   * PHP路由: Route::post('schedule', 'sys.Schedule/add')
   */
  @Post('schedule')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async add(@Body() data: AddDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: add

      return await this.scheduleService.add(data);
    } catch (error) {
      throw new BusinessException('add操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: PUT schedule/:id
   * PHP路由: Route::put('schedule/:id', 'sys.Schedule/edit')
   */
  @Put('schedule/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async edit(
    @Param('id') id: string,
    @Body() data: EditDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: edit

      return await this.scheduleService.edit(id, data);
    } catch (error) {
      throw new BusinessException('edit操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: PUT schedule/modify/status/:id
   * PHP路由: Route::put('schedule/modify/status/:id', 'sys.Schedule/modifyStatus')
   */
  @Put('schedule/modify/status/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async modifyStatus(
    @Param('id') id: string,
    @Body() data: ModifyStatusDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: modifyStatus

      return await this.scheduleService.modifyStatus(id, data['status']);
    } catch (error) {
      throw new BusinessException('modifyStatus操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: DELETE schedule/:id
   * PHP路由: Route::delete('schedule/:id', 'sys.Schedule/del')
   */
  @Delete('schedule/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async del(@Param('id') id: string): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: del

      return await this.scheduleService.del(id);
    } catch (error) {
      throw new BusinessException('del操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: GET schedule/datetype
   * PHP路由: Route::get('schedule/datetype', 'sys.Schedule/getDateType')
   */
  @Get('schedule/datetype')
  @ApiOperation({ summary: '自动任务' })
  async getDateType() {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: getDateType
      // 直接返回字典数据
      return ScheduleDict.getDateType();
    } catch (error) {
      throw new BusinessException('getDateType操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: PUT schedule/do/:id
   * PHP路由: Route::put('schedule/do/:id', 'sys.Schedule/doSchedule')
   */
  @Put('schedule/do/:id')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async doSchedule(
    @Param('id') id: string,
    @Body() data: DoScheduleDto,
  ): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: doSchedule

      return await this.scheduleService.doSchedule(id);
    } catch (error) {
      throw new BusinessException('doSchedule操作失败', error);
    }
  }

  /**
   * 自动任务
   * 路由: POST schedule/reset
   * PHP路由: Route::post('schedule/reset', 'sys.Schedule/resetSchedule')
   */
  @Post('schedule/reset')
  @UseGuards(JwtAuthGuard, RolesGuard)
  @ApiOperation({ summary: '自动任务' })
  async resetSchedule(@Body() data: ResetScheduleDto): Promise<ApiResponse> {
    try {
      // 基于PHP真实逻辑实现
      // PHP原始方法: resetSchedule

      return await this.scheduleService.resetSchedule();
    } catch (error) {
      throw new BusinessException('resetSchedule操作失败', error);
    }
  }
}
